package com.zsrt.java.day20161226.wrl.demo03;

import java.util.Scanner;

public class Work06 {
	
	/**
	 * 用switch完成：加减乘除求模的计算器，同时用if else 也要写一遍
	 */
/*	@Test
	public void w06Switch(){
		System.out.println("请输入第一个数：\n");
		Scanner sc = new Scanner(System.in);
		int index1 = sc.nextInt();
		System.out.println("请输入运算符：+  -  *  /  %\n");
		String index2 = sc.next();
		System.out.println("请输入第二个数：\n");
		int index3 = sc.nextInt();
		sc.close();
		switch (index2) {
		case "+":	System.out.println(index1+index3);		break;
		case "-":	System.out.println(index1-index3);		break;
		case "*":	System.out.println(index1*index3);		break;
		case "/":	System.out.println(index1/index3);		break;
		case "%":	System.out.println(index1%index3);		break;
		default:	System.out.println("您的输入有误！");		break;
		}
	}*/	
	public static void main(String[] args) {
		System.out.println("请输入第一个数：\n");
		Scanner sc = new Scanner(System.in);
		int number1 = sc.nextInt();
		System.out.println("请输入运算符：+  -  *  /  %\n");
		String flag = sc.next();
		System.out.println("请输入第二个数：\n");
		int number2 = sc.nextInt();
		sc.close();
		
//		int result = work06Switch(number1, flag, number2);
		int result = work06IfElse(number1, flag, number2);
		System.out.println(result);
	}

	/**
	 * 简易计算器
	 * @param number1	数1
	 * @param flag		运算符号
	 * @param number2	数2
	 * @return	结果
	 */
	public static int work06Switch(int number1,String flag,int number2){
		int result = 0;
		switch (flag) {
			case "+":	result = number1+number2;		break;
			case "-":	result = number1-number2;		break;
			case "*":	result = number1*number2;		break;
			case "/":	result = number1/number2;		break;
			case "%":	result = number1%number2;		break;
			default:	result = 0;		   break;
		}
		return result;
	}
	
	public static int work06IfElse(int number1, Object flag, int number2){
		int result = 0;
		if("+".equals(flag)){
			result = number1 + number2;
		}else if("-".equals(flag)){
			result = number1 - number2;
		}else if("*".equals(flag)){
			result = number1 * number2;
		}else if("/".equals(flag)){
			result = number1 / number2;
		}else if("%".equals(flag)){
			result = number1 % number2;
		}else{	
			result = 0;	
		}
		return result;
	}
}
